/**
 * Class:	AdviserEvaluationManager
 * 
 * Methods:
 * 
 *      boolean insertAdviserEvaluation( int idActivity,
 *      								 AdviserEvaluation adviserEvaluation );
 *      
 *      AdviserEvaluation searchAdviserEvaluation( int activityID );
 *      
 *      boolean deleteAdviserEvaluation( int activityID );
 * 
 * Remarks:
 * 
 * 		This class should not be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import models.AdviserEvaluation;

public class AdviserEvaluationManager extends DatabaseManager {
	
	/**
	 * Constructors
	 */
	
	public AdviserEvaluationManager() {}
    
    
    
    /**
     *  Insert Adviser Evaluation
     *  	- inserts an adviser evaluation into the
     *  	  evaluationAdviser database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  adviser evaluation belongs
     *		- AdviserEvaluation adviserEvaluation:
     *				the adviserEvaluation to be added
     *	
     *	Returns
     *		- boolean true: adviserEvaluation is
     *						successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertAdviserEvaluation( int activityID,
    								 AdviserEvaluation adviserEvaluation ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "INSERT INTO `evaluationAdviser`" +
    								   " (`activityID`, `dateFiled`," +
    								   " `question1`, `question2`," +
    								   " `question3`, `question4`)" +
    								   " VALUES (?, ?, ?, ?, ?, ?)" );

    		preparedStatement.setInt( 1, activityID );
    		preparedStatement.setString( 2, adviserEvaluation
    										.getDateFiled().toString() );
    		for ( int i = 0; i < 4; i++ ) {
    			
				preparedStatement.setString( (i + 3), adviserEvaluation
								   				   .getStrAnswers()[i] );
			}
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchAdviserEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
	
	
	
	/**
	 *  Search Adviser Evaluation
	 *  	- gets the adviserEvaluation in a certain activity
	 *  	  from the evaluationAdviser database
	 *  
	 *  Parameters
	 *		- int activityID: the ID of the activity the adviserEvaluation
	 *						  is from 
	 *	
	 *	Returns
	 *		- null: no adviserEvaluation is found
	 * 		- AdviserEvaluation: adviserEvaluation is found
	 */
	AdviserEvaluation searchAdviserEvaluation( int activityID ) {
		
		/* Declaration of Local Variables */
		AdviserEvaluation adviserEvaluation = null;
		
		try {
    		/* Declaration of Block Variables */
			Statement statement;
			ResultSet resultSet;
			
			connect();
			
			statement = getConnection().createStatement();
			
			resultSet = statement
						.executeQuery( "SELECT * FROM `evaluationAdviser`" +
									   " WHERE `activityID` = " + activityID );
			
			while ( resultSet.next() ) {
				String[] strAnswers = new String[5];
				
				for ( int i = 0; i < 4; i++ ) {
					strAnswers[i] = resultSet.getString( "question" +
															 (i + 1) );
				}
				adviserEvaluation = new AdviserEvaluation
						( parseDate( resultSet.getDate( "dateFiled" ) ),
						  strAnswers );
			}
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}
		finally {
	    	if ( getConnection() != null ) {
	    		disconnect();
	    	}
		}
		return adviserEvaluation;
	}
    
    
    
    /**
     *  Update Adviser Evaluation
     *  	- update an adviser evaluation in the
     *  	  evaluationAdviser database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  adviser evaluation belongs
     *		- AdviserEvaluation overwrite: the new version
     *				of the adviserEvaluation to be updated
     *	
     *	Returns
     *		- boolean true: adviserEvaluation is
     *						successfully updated
     * 		- boolean false: updating failed
     */
    boolean updateAdviserEvaluation( int activityID,
    								 AdviserEvaluation overwrite ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "UPDATE `evaluationAdviser` SET" +
    								   " `dateFiled` = ?, `question1` = ?," +
    								   " `question2` = ?, `question3` = ?," +
    								   " `question4` = ?)" +
    								   " WHERE `activityID` = ?" );

    		preparedStatement.setString( 1, overwrite.getDateFiled().toString() );
    		for ( int i = 0; i < 4; i++ ) {
				preparedStatement.setString( (i + 2),
											 overwrite.getStrAnswers()[i] );
			}
    		preparedStatement.setInt( 6, activityID );
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchAdviserEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Adviser Evaluation
     *  	- deletes an adviserEvaluation from the
     *  	  evaluationAdviser database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  adviserEvaluation belongs
     *	
     *	Returns
     *		- boolean true: adviserEvaluation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteAdviserEvaluation( int activityID ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		if ( searchAdviserEvaluation( activityID ) != null ) {
    			preparedStatement = getConnection()
        							.prepareStatement( "DELETE FROM" +
        											   " `evaluationaAdviser`" +
        											   " WHERE `activityID`" +
        											   " = ?" );
	    		
	    		preparedStatement.setInt( 1, activityID );
	    		preparedStatement.executeUpdate();
	    		
				disconnect();
				
	    		if ( searchAdviserEvaluation( activityID ) == null ) {
	    			return true;
	    		}
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
}
